//
//  CTCBirdShowViewController.m
//  jieshuibao
//
//  Created by 边文辉 on 2016/12/26.
//  Copyright © 2016年 com.jieshuibao1. All rights reserved.
//

#import "CTCBirdShowViewController.h"

@interface CTCBirdShowViewController ()<CAAnimationDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (nonatomic, strong) CAKeyframeAnimation *animation;
@property (weak, nonatomic) IBOutlet UIImageView *redPackage;
@property (weak, nonatomic) IBOutlet UIButton *cancelBtn;

//@property (nonatomic, assign) double money;
//@property (nonatomic, assign) double peekMoney;
//@property (nonatomic, assign) double rewardMoney;
//@property (nonatomic, assign) double redMoney;
@property (nonatomic, assign, getter=isFinished) BOOL finished;

/** 次数 */
@property (nonatomic, assign) NSUInteger number;

/** 金额 */
@property (nonatomic, assign) double amount;


@end

@implementation CTCBirdShowViewController

+ (instancetype)shareVC
{
    static CTCBirdShowViewController *_instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[CTCBirdShowViewController alloc] init];
    });
    
    return _instance;
}

+ (BOOL)display
{
    
        [RequestTool POST:kBirdShowUrl
               parameters:nil
                 progress:nil
                  success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                      
                      DeLog(@"咨询次数 ：responseObject %@", responseObject);
                      
                      NSInteger code = [responseObject[@"code"] integerValue];
                      if (code == 1) {
                          NSUInteger number = [responseObject[@"data"][@"total"] integerValue];
                          
                          double totalPrice = [responseObject[@"data"][@"totalPrice"] floatValue];
                          
                          if (totalPrice > 0) {
                              
                              [CTCBirdShowViewController shareVC].number = number;
                              [CTCBirdShowViewController shareVC].amount = totalPrice;
                          }
                      }
                      
                  }
                  failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                      DeLog(@"err : %ld -- %@", (long)error.code, error);
                  }
         ];
    
    return NO;
}

+ (void)show
{
    CTCBirdShowViewController *shareVC = [CTCBirdShowViewController shareVC];
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    [keyWindow addSubview:shareVC.view];
}


-(void)setAmount:(double)amount
{
    _amount = amount;
    if (amount > 0) {
        UIImage *image = [UIImage imageWithAmount:amount number:_number centerY:236];
        dispatch_async(dispatch_get_main_queue(), ^{
            self.redPackage.image = image;
        });
        
        [self show];
    }
}



- (void)show
{
    [[UIApplication sharedApplication].keyWindow addSubview:self.view];
}

#pragma mark - viewDidLoad
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:.6f];
    self.view.frame = [UIApplication sharedApplication].keyWindow.bounds;
    
    self.redPackage.hidden = YES;
 
    _number = 0;
    
    NSMutableArray *images = [NSMutableArray array];
    for (int i = 1; i < 15; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"bird-%d", i]];
//        CGImageRef cgimg = image.CGImage;
//        [images addObject:(__bridge id _Nonnull)(cgimg)];
        [images addObject:image];
    }

    _imageView.animationImages = images;
    _imageView.animationDuration = 2.f;
    _imageView.animationRepeatCount = 1;
    
//    //创建CAKeyframeAnimation
//    _animation = [CAKeyframeAnimation animationWithKeyPath:@"contents"];
//    _animation.duration = 2.f;
//    _animation.delegate = self;
//    _animation.values = images;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    _finished = NO;
    [self showAnima];
}

- (void)showAnima
{
//    [_imageView.layer addAnimation:_animation forKey:@"imgAnimat"];
    
    _imageView.hidden = NO;
    _redPackage.hidden = YES;
    _cancelBtn.hidden = YES;
    
    [self.imageView startAnimating];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        _imageView.hidden = YES;
        _redPackage.hidden = NO;
        _cancelBtn.hidden = NO;
        [self packagEnlarge];
    });
}


- (void)packagEnlarge
{
    [UIView animateWithDuration:.8f animations:^{
//        _redPackage.frame = self.view.bounds;
//        _redPackage.layer.frame = self.view.bounds;
        _redPackage.layer.bounds = CGRectMake(0, 0, 221, 319);
        _redPackage.layer.position = self.view.center;

    } completion:^(BOOL finished) {
        _finished = YES;
    }];
}



- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
//    [_imageView.layer removeAnimationForKey:@"imgAnimat"];
//    self.imageView.image = [UIImage imageNamed:@"bird_red_package"];
}



- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    if (_finished) {
        [self disMiss];
    }
}

- (void)disMiss
{
    [UIView animateWithDuration:.5f animations:^{
        CGFloat x = self.view.centerX;
        CGFloat y = 0 - _redPackage.height * .5f;
        _redPackage.layer.position = CGPointMake(x, y);
    } completion:^(BOOL finished) {
        
        [self.view removeFromSuperview];
    }];
    
}

- (IBAction)cancel:(UIButton *)sender {
    [self disMiss];
}


@end
